﻿using AiShow.BLL;
using AiShow.IBLL;
using AiShow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AiShow.WebSite
{
    /// <summary>
    /// 视频列表页
    /// </summary>
    public partial class Videos : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 查询用户资料
                int otherId = int.Parse(Request.QueryString["id"]);
                IUserManager userManager = new UserManager();
                TBUsers other = userManager.GetUser(otherId).ReadResult();
                // 显示用户资料
                this.lblNickName.Text = other.NickName ?? $"用户{other.Id}";
                switch (other.Sex)
                {
                    case 1:
                        this.lblSex.Text = "小哥哥";
                        this.lblSex.CssClass += " bg-color-blue";
                        break;
                    case 2:
                        this.lblSex.Text = "小姐姐";
                        this.lblSex.CssClass += " bg-color-pink";
                        break;
                    case 0:
                    default:
                        this.lblSex.Text = "未知";
                        this.lblSex.CssClass += " bg-color-black";
                        break;
                }
                this.lblAddress.Text = other.Address ?? "未知地";
                this.lblBirthday.Text = other.Birthday.HasValue ? other.Birthday.Value.ToString("yyyy-MM-dd") : "未设置";
                this.lblHobby.Text = other.Hobby ?? "未添加";
                // 如果为本人，则隐藏好友及聊天按钮
                int selfId = IdentityManager.ReadUser().Id;
                if (selfId.Equals(otherId))
                {
                    this.btnFriend.Enabled = false;
                    this.btnMessage.Enabled = false;
                    this.btnFriendBlock.Visible = false;
                }
                // 判断是否是好友
                IFriendManager friendManager = new FriendManager();
                bool isFriend = friendManager.IsFriend(selfId, otherId);
                //简化
                this.btnFriend.Visible = !isFriend;//是好友，隐藏
                this.btnMessage.Visible = isFriend;//显示聊天
                //if (isFriend)
                //{
                //    this.btnFriend.Enabled = false;
                //    this.btnFriend.Visible = false;
                //}
                //else
                //{
                //    this.btnMessage.Enabled = false;
                //    this.btnMessage.Visible = false;
                //}
                // 展示视频列表
                IVideoManager videoManager = new VideoManager();
                List<TBVideos> videos = videoManager.GetVideosByUserId(otherId).ReadResult();
                rptVideos.DataSource = videos;
                rptVideos.DataBind();
            }
        }

        /// <summary>
        /// 添加好友事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFriend_Click(object sender, EventArgs e)
        {
            // 不能添加自己为好友
            int selfId = IdentityManager.ReadUser().Id;
            int otherId = int.Parse(Request.QueryString["id"]);
            if (selfId.Equals(otherId))
            {
                ClientScript.RegisterStartupScript(GetType(), "frdfail", $"alert('不能添加自己为好友！')", true);
                return;
            }
            // 添加好友
            IFriendManager friendManager = new FriendManager();
            bool friendResult = friendManager.AddFriend(selfId, otherId);
            if (!friendResult)
            {
                // 提示添加失败
                ClientScript.RegisterStartupScript(GetType(), "frderror", $"alert('添加失败！');location='Videos.aspx?to={otherId}'", true);
            }
            else
            {
                // 提示并跳转页面
                ClientScript.RegisterStartupScript(GetType(), "frdsuccess", $"alert('添加成功');location='Chat.aspx?to={otherId}'", true);
            }
        }

        /// <summary>
        /// 立即聊天
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMessage_Click(object sender, EventArgs e)
        {
            int otherId = int.Parse(Request.QueryString["id"]);
            Response.Redirect($"Chat.aspx?to={otherId}");
        }
    }
}